const defaultState = {
    // 初始化cartID
    cartID:[],
    // 初始化一级菜单信息
    menuOne:[],
    // 展示购物车
    showCart:false,
    // 根据sortID展示菜品
    showSortFood:[],
    // 一级菜单ID
    SortOID:1,
    // 初始化商家的信息
    initStoreMsg:{},
    // 商家所有的菜品
    allFood:[],
    // 目前被选中的菜品规格
    nowSelectFood:{},
    seletedID:0,
    // 获取到了一个商店的菜单信息后，将数据保存到这里
    beSelectKind:[],//id,name,price,num
    isBeSelect:[
        {id:1,state:false,nowIndex:0,nowFood:""},
        {id:2,state:false,nowIndex:0,nowFood:""},
        {id:3,state:false,nowIndex:0,nowFood:""}],
    
        //定义用户是否正在选择菜品规格
    isSelectFood:false,
    // 根据Nav值切换点餐、评论、商家
    selectedNav:"order",
    // 定义一个初始scollTop值
    offsetTop:0,
    // 定义一个初始商家菜品信息
    foodCanSelect:[
        {name:"辣度",value:["不要辣","微辣","中辣","重辣","变态辣"]},
        {name:"米饭",value:["带米饭","不带米饭"]},
        {name:"加菜",value:["千张","菠菜","娃娃菜","油豆泡","鸡肉"]}
    ]

}

export default function(state=defaultState,actions){
    let newState = JSON.parse(JSON.stringify(state));
    switch(actions.type){

        case "initMenuOne":
            newState.menuOne = actions.payload
            break;
        case "showCart":
            newState.showCart = actions.payload;
            break;
        case "order":
            newState.selectedNav = "order"
            break;
        case "comment":
            newState.selectedNav = "comment"
            break;
        case "merchant":
            newState.selectedNav = "merchant"
            break;
        case "selectNow":
            // 点击选择菜品的时候，传一个菜品的ID进来，根据ID来给当前的
            // 数组beSelectKind:[],id,name,price,num 添加对应id内容，
            newState.isSelectFood = true;            
            newState.seletedID = actions.payload.id;
            newState.nowSelectFood = actions.payload.data[0]
            break;
        case "doneSelect":
            newState.isSelectFood = false;
            newState.beSelectKind.filter((v)=>v.id===actions.productID)[0].kind1 = state.isBeSelect[0].nowFood || state.foodCanSelect[0].value[0];
            newState.beSelectKind.filter((v)=>v.id===actions.productID)[0].kind2 = state.isBeSelect[1].nowFood || state.foodCanSelect[1].value[0];
            newState.beSelectKind.filter((v)=>v.id===actions.productID)[0].kind3 = state.isBeSelect[2].nowFood || state.foodCanSelect[2].value[0];
            newState.beSelectKind.filter((v)=>v.id===actions.productID)[0].num += 1;
            newState.cartID.push({id:actions.productID,cartID:actions.cartID})
            
            break;
        case "change0":
            newState.isBeSelect[0].state = true;
            newState.isBeSelect[0].nowIndex = actions.payload;
            newState.isBeSelect[0].nowFood = actions.selectFood;
            newState.beSelectKind.filter((v)=>v.id===state.seletedID)[0].kind1 = actions.selectFood;
            break
        case "change1":
            newState.isBeSelect[1].state = true;
            newState.isBeSelect[1].nowIndex = actions.payload
            newState.isBeSelect[1].nowFood = actions.selectFood;
            newState.beSelectKind.filter((v)=>v.id===state.seletedID)[0].kind2 = actions.selectFood;
            break
        case "change2":
            newState.isBeSelect[2].state = true;
            newState.isBeSelect[2].nowIndex = actions.payload
            newState.isBeSelect[2].nowFood = actions.selectFood;
            newState.beSelectKind.filter((v)=>v.id===state.seletedID)[0].kind3 = actions.selectFood;
            break
        case "data":
            newState.beSelectKind = actions.payload;
            break;
        case "changeFoodNum":
            newState.seletedID = actions.id;
            newState.beSelectKind.forEach(v => {
                if(parseInt(v.id)===parseInt(actions.id)){
                    v.num += actions.step;
                }
            })
            break;
        case "sendAllFood":
            newState.allFood = actions.payload
            break;
        case "top":
            newState.offsetTop = actions.payload;
            break;
        // 初始化商家信息
        case "init":
            newState.initStoreMsg = actions.payload
            break;
        case "changeSortTID":
            newState.SortOID = actions.payload
            break;
        case "showSortFood":
            newState.showSortFood = actions.payload
            break;
        default:
            break;
        }
        
        

    return newState
}